<template>
  {{ ruleForm }}
  <el-card style="max-width: 480px; margin: 250px auto;">
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
      <el-form-item label="用户名" prop="accountName">
        <el-input v-model="ruleForm.accountName" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" show-password />
      </el-form-item>


      <el-form-item label="记住密码?">
        <!-- <el-input v-model="ruleForm.password"  show-password /> -->
        <el-checkbox v-model="ruleForm.remember"></el-checkbox>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          登录
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'

import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import { useCounterStore } from '@/stores/counter';//
import router from '@/router';

const pinia = useCounterStore()//实例化pinia



const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({//来自接口文档swagger
  "accountName": "",
  "password": "",
  "remember": false
})



const rules = reactive<FormRules>({
  accountName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 15, message: '用户名的长度应为2-15位', trigger: 'blur' },
  ], password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 2, max: 15, message: '密码的长度应为2-15位', trigger: 'blur' },
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      //调用登录接口
      axios.post('http://localhost:5080/api/Account/Login', {
        "accountName": ruleForm.accountName,
        "password": ruleForm.password
      }).then(res => {
        if (res.data.code == 200) {//登陆成功
          ElMessage.success(res.data.msg);
          //把后端返回的loginDto信息保存到pinia

          pinia.loginDto.accountName = res.data.data.accountName
          pinia.loginDto.realName = res.data.data.realName
          pinia.loginDto.roleName = res.data.data.roleName
          pinia.loginDto.jwtToken = res.data.data.jwtToken

          //判断是否把用户名 密码保存到pinia的jizhu对象中去
          if (ruleForm.remember == true) {//1.登录成功后,要判断是否把用户名和密码保存到pinia里去
            pinia.jizhu.accountName = ruleForm.accountName
            pinia.jizhu.password = ruleForm.password
            pinia.jizhu.remember = ruleForm.remember
          } else {
            pinia.jizhu.accountName = ''
            pinia.jizhu.password = ''
            pinia.jizhu.remember = false
          }

          //跳转到主页面
          router.push("main");

        } else {
          ElMessage.error(res.data.msg);
        }
      })


    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const getRemenber = () => {
  if (pinia.jizhu.remember == true) {//如果上次记住了密码,本次从pinia里读出用户名和密码,反填登录页用户名密码文本框里
    ruleForm.accountName = pinia.jizhu.accountName
    ruleForm.password = pinia.jizhu.password
    ruleForm.remember = pinia.jizhu.remember
  } else {
    ruleForm.accountName = ''
    ruleForm.password = ''
    ruleForm.remember = false
  }

}

onMounted(() => {
getRemenber()
})

</script>